package com.bafa.objects;

import java.io.Serializable;
import java.util.*;

import com.bafa.dbcache.LeagueCache;
import com.bafa.objects.schema.*;
import com.bafa.services.RetrieveServices;
import com.bafa.util.comparators.LeagueComparator;
import com.bafa.util.comparators.MatchByDateComparator;
import com.bafa.util.comparators.TopScorerComparator;
import com.bafa.objects.team.Team;
public class Division implements Serializable{

	
	private boolean completed;
	private int division;
	private LeagueRules leagueRules;
	private List matches = new ArrayList();
	private TreeMap scorers = new TreeMap();
	private int seasonId;
	private List teams = new ArrayList();
	
	
	public Division() {
		// TODO Auto-generated constructor stub
	}
	
	
	public Division(com.bafa.objects.schema.Division division){
		this.division = division.getDivision();
		this.seasonId = division.getSeasonId();
		for(int i=0;i<division.getScorer().size();i++){
			addScorer(new Scorer(division.getScorer().get(i)));
		}
		
		for(int i=0;i<division.getTeam().size();i++){
			addTeam(division.getTeam().get(i));
		}
		
		for(int i=0;i<division.getMatch().size();i++){
			addMatch(division.getMatch().get(i));
		}
		
		if(division.getLeagueRules()!=null)setLeagueRules(new LeagueRules(division.getLeagueRules()));
		
		setCompleted((division.isComplete()!=null)?division.isComplete():false);
		
	}
	
	
	public Division(int division,int seasonId){
		this.division = division;
		this.seasonId = seasonId;
		
	}
	public void addMatch(int match){
		matches.add(match);
	}
	public void addScorer(Scorer scorer){
		if(!scorer.getName().equalsIgnoreCase("o.g.")&&
				!scorer.getName().equalsIgnoreCase("own goal")&&
				!scorer.getName().equalsIgnoreCase("og"))
		scorers.put(scorer.getName(),scorer);
	}
	public void addTeam(int team){
		teams.add(team);
	}
	public Match[] disputedResultsArray(){
		Match[] allMatches = matchArray();
		TreeSet list = new TreeSet(new MatchByDateComparator());
		for(int i=0;i<allMatches.length;i++){
			Match match = allMatches[i];
			if(match.isBlocked()){
				list.add(match);
			}
		}
		return (Match[])list.toArray(new Match[list.size()]);
	}
	
	public Match[] dueMatchArray(){
		Match[] allMatches = matchArray();
		TreeSet list = new TreeSet(new MatchByDateComparator());
		for(int i=0;i<allMatches.length;i++){
			Match match = allMatches[i];
			if(match.isResultDue()){
				list.add(match);
			}
		}
		return (Match[])list.toArray(new Match[list.size()]);
	}
	
	public int getDivision() {
		return division;
	}
	
	public LeagueRules getLeagueRules(){
		return leagueRules;
	}
	public List getMatches() {
		return matches;
	}
	
	public Scorer getScorer(int index) {
		Scorer[] scorer = (Scorer[])getScorers().toArray(new Scorer[getScorers().size()]);
		return scorer[index];
	}
	
	public TreeSet getScorers() {
		TreeSet set = new TreeSet(new TopScorerComparator());
		
		set.addAll(scorers.values());
		return set;
	}
	
	public int getSeasonId() {
		return seasonId;
	}
	
	public List getTeams() {
		return teams;
	}
	
	public boolean isCompleted() {
		return completed;
	}
	
	public Match[] matchArray(){
		Match[] matchArray = new Match[matches.size()];
		for(int i=0;i<matches.size();i++){
			int matchId = (Integer)matches.get(i);
			Match match = RetrieveServices.getMatch(matchId);
			matchArray[i]=match;
		}
		
		return matchArray;
	}
	
	public Match[] notPlayedMatchArray(){
		Match[] allMatches = matchArray();
		TreeSet list = new TreeSet(new MatchByDateComparator());
		for(int i=0;i<allMatches.length;i++){
			Match match = allMatches[i];
			if(!match.isPlayed()){
				list.add(match);
			}
		}
		return (Match[])list.toArray(new Match[list.size()]);
	}
	
	public Match[] playedMatchArray(){
		Match[] allMatches = matchArray();
		TreeSet list = new TreeSet(new MatchByDateComparator());
		for(int i=0;i<allMatches.length;i++){
			Match match = allMatches[i];
			if(match.isPlayed()){
				list.add(match);
			}
		}
		return (Match[])list.toArray(new Match[list.size()]);
	}
	
	public Match[] playedUnconfirmedMatchesArray(){
		Match[] allMatches = matchArray();
		TreeSet list = new TreeSet(new MatchByDateComparator());
		for(int i=0;i<allMatches.length;i++){
			Match match = allMatches[i];
			if(match.isPlayed()&&!match.isConfirmed()){
				list.add(match);
			}
		}
		return (Match[])list.toArray(new Match[list.size()]);
	}
	
	public void setCompleted(boolean completed) {
		this.completed = completed;
	}
	
	
	public void setIsCompleted(int numGames){
		int numTeams = teams.size();
		int shouldPlay = ((numTeams - 1) *leagueRules.getNumPlayEach())*(numTeams/2);
		if(numGames==shouldPlay)
			completed=true;
		else
			completed=false;
	}
	public void setLeagueRules(LeagueRules leagueRules){
		this.leagueRules = leagueRules;
	}
	public Team[] teamArray(){
		
		TreeSet set = new TreeSet<Team>(new LeagueComparator());
		for(int i=0;i<teams.size();i++){
			int teamId = (Integer)teams.get(i);
			Team team = RetrieveServices.getTeam(""+teamId);
			//team.setSeasonToCheck(seasonId);
			set.add(team);
			
		}
		Team[] teamArray = (Team[])set.toArray( new Team[teams.size()]);
		return teamArray;
	}
	
	public int[] teamIdArray(){
		int[] teamArray = new int[teams.size()];
		for(int i=0;i<teams.size();i++){
			int teamId = (Integer)teams.get(i);
			teamArray[i]=teamId;
		}
		
		return teamArray;
	}
	

	public Scorer[] topScorers(){
		return (Scorer[])getScorers().toArray(new Scorer[scorers.size()]);
	}

	public String toString(){
		
		return "Number of teams: "+teams.size()+"\n Number of Matches: "+matches.size();
	}

}
